package com.ruoyi.api.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.vim.domain.ImServiceAllocationPolicy;
import com.ruoyi.vim.service.IImServiceAllocationPolicyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客服分配策略Controller
 *
 * @author wgp
 * @since 2022-12-14
 */
@RestController
@RequestMapping("/vim/policy")
public class ImServiceAllocationPolicyController extends BaseController<ImServiceAllocationPolicy> {
    @Autowired
    private IImServiceAllocationPolicyService imServiceAllocationPolicyService;

    /**
     * 查询客服分配策略列表
     */
    @PreAuthorize("@ss.hasPermi('vim:policy:list')")
    @GetMapping("/list")

    /**
     * 列表
     * @param imServiceAllocationPolicy 实体
     * @param pageNum 页码
     * @param pageSize 每页多少条
     */
    public TableDataInfo list(ImServiceAllocationPolicy imServiceAllocationPolicy, Long pageNum, Long pageSize) {
        QueryWrapper<ImServiceAllocationPolicy> queryWrapper = getQueryWrapper(imServiceAllocationPolicy);
        IPage<ImServiceAllocationPolicy> iPage = imServiceAllocationPolicyService.page(new Page<ImServiceAllocationPolicy>(pageNum, pageSize), queryWrapper);
        return getDataTable(iPage);
    }

    /**
     * 导出客服分配策略列表
     */
    @PreAuthorize("@ss.hasPermi('vim:policy:export')")
    @Log(title = "客服分配策略", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ImServiceAllocationPolicy imServiceAllocationPolicy) {
        QueryWrapper<ImServiceAllocationPolicy> queryWrapper = getQueryWrapper(imServiceAllocationPolicy);
        List<ImServiceAllocationPolicy> list = imServiceAllocationPolicyService.list(queryWrapper);
        ExcelUtil<ImServiceAllocationPolicy> util = new ExcelUtil<>(ImServiceAllocationPolicy.class);
        util.exportExcel(response, list, "客服分配策略数据");
    }

    /**
     * 获取客服分配策略详细信息
     */
    @PreAuthorize("@ss.hasPermi('vim:policy:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(imServiceAllocationPolicyService.getById(id));
    }

    /**
     * 新增客服分配策略
     */
    @PreAuthorize("@ss.hasPermi('vim:policy:add')")
    @Log(title = "客服分配策略", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ImServiceAllocationPolicy imServiceAllocationPolicy) {
        return toAjax(imServiceAllocationPolicyService.save(imServiceAllocationPolicy));
    }

    /**
     * 修改客服分配策略
     */
    @PreAuthorize("@ss.hasPermi('vim:policy:edit')")
    @Log(title = "客服分配策略", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ImServiceAllocationPolicy imServiceAllocationPolicy) {
        return toAjax(imServiceAllocationPolicyService.updateById(imServiceAllocationPolicy));
    }

    /**
     * 删除客服分配策略
     */
    @PreAuthorize("@ss.hasPermi('vim:policy:remove')")
    @Log(title = "客服分配策略", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(imServiceAllocationPolicyService.removeBatchByIds(Arrays.asList(ids)));
    }

    private QueryWrapper<ImServiceAllocationPolicy> getQueryWrapper(ImServiceAllocationPolicy imServiceAllocationPolicy) {
        QueryWrapper<ImServiceAllocationPolicy> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(imServiceAllocationPolicy.getServiceId())) {
            queryWrapper.eq("service_id", imServiceAllocationPolicy.getServiceId());
        }
        if (StringUtils.isNotEmpty(imServiceAllocationPolicy.getAllocationPolicy())) {
            queryWrapper.eq("allocation_policy", imServiceAllocationPolicy.getAllocationPolicy());
        }
        if (StringUtils.isNotEmpty(imServiceAllocationPolicy.getStatus())) {
            queryWrapper.eq("status", imServiceAllocationPolicy.getStatus());
        }
        return queryWrapper;
    }
}
